home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 090 / resort.c < prev    next >
Text File  |  1985-06-03  |  8KB  |  284 lines

  1. #define ERROR -1
  2. /*-------------------------------------------------------------
  3. RESORT is a utility which maintains a user dictionary SPELL.DIC
  4. in alpha order. This works in conjunction with the utility
  5. SPELL.COM, by Michael C. Adler, as modified for the Osborne 
  6. User Groups.
  7.  
  8. RESORT is Copyright (C) 1983 by Donald G. Krantz. No commercial
  9. use may be made of this program without permission of Author.
  10.  
  11. Limited license is granted for non-commercial distribution of
  12. this program in either source or object form.
  13. ---------------------------------------------------------------
  14. -------------------------------------------------------------*/
  15.  
  16. #define VERSION    "RESORT Version 1.1 (C) 1984 Donald G. Krantz"
  17. #include "stdio.h"
  18.  
  19. /*------------------------------------------------------------
  20. stucture node is the basic data type used to describe the 
  21. sorted array of words.
  22. ------------------------------------------------------------*/
  23.  
  24. struct node {
  25.     struct node *lower;
  26.     struct node *higher;
  27.     char *word;
  28.         };
  29.  
  30. struct node *root;    /* root is the root of the binary tree    */
  31.  
  32. FILE *inptr;        /* file descriptor to input files    */
  33. FILE *outptr;        /* file descriptor to output file    */
  34.  
  35. main( argc, argv )
  36.    int argc;
  37.    char *argv[];
  38. {
  39.    char tempfile[ 20 ];        /* temporary text filespec    */
  40.    char bakfile[ 20 ];        /* .BAK filespec        */
  41.  
  42.     printf("\n\s\n", VERSION );
  43. /* check command line argument */
  44.     if( argc != 2 )        /* one argument only...        */
  45.     {
  46.         printf("\nERROR - Use:\nRESORT d:filename.typ\n\n");
  47.         exit( 1 );
  48.     }
  49. /* open input file */
  50.     if( (inptr = fopen( argv[1], "r", 512 )) == NULL )
  51.     {
  52.         printf("\nERROR - Can't open file \s\n",argv[1]);
  53.         exit( 1 );
  54.     }
  55. /* make .BAK and .$$$ file spec and open temps */
  56.     makebak( argv[ 1 ], bakfile, tempfile );
  57.     unlink( tempfile );            /* dump old temps */
  58.     if( (outptr = fopen( tempfile, "w" ) ) == NULL )
  59.     {
  60.         printf( "\nERROR - Can't open temporary file\n" );
  61.         exit( 1 );
  62.     }
  63. /* sort input file and strip tildes, transfer to temp file */
  64.     root = sort( NULL );
  65. /* change temp filename to original, original to .BAK */
  66.     fclose( inptr );            /* close input    */
  67.     if( fclose( outptr ) == ERROR)        /* close temp     */
  68.     {
  69.         printf( "\nERROR - Disk full - aborting\n" );
  70.         unlink( tempfile );
  71.         exit( 1 );
  72.     }
  73.     unlink( bakfile );            /* dump old .BAK  */
  74. /* rename: rename( new_file_spec, old_file_spec ) */
  75.     rename( bakfile, argv[1] );        /* make new .BAK  */
  76.     rename( argv[1], tempfile );        /* make new text  */
  77.     if( root == NULL )            /* no new words   */
  78.         exit( 0 );            /* so quit      */
  79. /* sort .DIC file into binary tree */
  80.     if( (inptr = fopen( "SPELL.DIC", "r" )) == NULL )
  81.     {
  82.         printf( "\nERROR - Can't open dictionary file\n" );
  83.         exit( 1 );
  84.     }
  85.     root = sort2( root );            /* sort .DIC     */
  86.     fclose( inptr );            /* dump .DIC     */
  87.         /* re-write .DIC file    */
  88.     if( (inptr = fopen( "SPELL.DIC", "w", 512 )) == NULL )
  89.     {
  90.         printf( "\nERROR - Can't write to dictionary file");
  91.         exit( 1 );
  92.     }
  93. /* write sorted tree to .DIC file */
  94.     print( root );
  95.     if( fclose( inptr ) == ERROR )
  96.     {
  97.         printf("\nERROR - Disk full - Recheck SPELL.DIC\n");
  98.         exit( 1 );
  99.     }
  100. }
  101.  
  102.  
  103. /*---------------------------------------------------------------
  104. makebak() creates filespecs for a temporary file and a backup file
  105. to be used in filtering applications like RESORT. It's kind of
  106. a generic function.
  107. ---------------------------------------------------------------*/
  108.  
  109. makebak( orgfile, bakfile, tempfile )
  110.    char *orgfile, *bakfile, *tempfile;
  111. {
  112.     strcpy( tempfile, orgfile );
  113.     strcpy( bakfile, orgfile );
  114.     if( index( tempfile, '.' )  == NULL )    /*.TYP spec'ed? */
  115.     {                    /* no        */
  116.         strcat( tempfile, ".$$$" );
  117.         strcat( bakfile, ".BAK" );
  118.     }
  119.     else                    /* yes        */
  120.     {
  121.         strcpy( index( tempfile, '.'), ".$$$");
  122.         strcpy( index( bakfile, '.'), ".BAK");
  123.     }
  124. }
  125.  
  126.  
  127. /*---------------------------------------------------------------
  128. sort() reads the input file and puts the words with leading 
  129. tildes into the binary tree.
  130. ---------------------------------------------------------------*/
  131.  
  132. struct node *
  133. sort( p )
  134.    struct node *p;
  135. {
  136.    static char w[100];
  137.    register int i;
  138.    static int c;
  139.  
  140.     while( TRUE )
  141.     {
  142. /* find tilde    */
  143.         while((((c = getcc( inptr )) & 0x7F) != '~') && 
  144.           (c != ERROR))
  145.             ;
  146. /* accumulate subsequent word into 'w' */
  147.         i = 0;        /* i = letter count in word */
  148.         while( (i == 0) || (isalpha( c ) ) )
  149.         {
  150.             c = getcc( inptr );
  151.             if( c == ERROR )
  152.                 return( p );
  153.             c = toupper( 0x7F & c );
  154.             if( isalpha( c ) == FALSE )
  155.                 break;
  156.             w[ i++ ] = c;
  157.         }
  158.         if( i == 0 )
  159.             continue;
  160.         w[ i ] = '\0';
  161. /* put word into binary tree */
  162.         p = treeload( w, p );
  163.     }
  164. }
  165.  
  166.  
  167. /*---------------------------------------------------------------
  168. sort2() reads the .DIC file and puts the words into the binary 
  169. tree.
  170. ---------------------------------------------------------------*/
  171.  
  172. struct node *
  173. sort2( p )
  174.    struct node *p;
  175. {
  176.    static char w[100];
  177.    register int i;
  178.    static int c;
  179.  
  180.     while( TRUE )
  181.     {
  182.         i = 0;        /* i=letter count in word */
  183. /* loop for alphabetic data, skip non-alpha */
  184.         while((i == 0) || (isalpha( c )))
  185.         {
  186.             c = getc( inptr );
  187.             if( c == ERROR )
  188.                 return( p );
  189.             c = toupper( 0x7F & c );
  190.             if( isalpha( c ) == FALSE )
  191.                 break;
  192.             w[ i++ ] = c;
  193.         }
  194.         if( i == 0 )
  195.             continue;
  196.         w[ i ] = '\0';
  197. /* put word into binary tree */
  198.         p = treeload( w, p );
  199.     }
  200. }
  201.  
  202.  
  203. /*-------------------------------------------------------------
  204. treeload( w, p ) loads the tree with words, placing them in
  205. alphabetical order.
  206. -------------------------------------------------------------*/
  207.  
  208. struct node *
  209. treeload( w, p )
  210.    char *w;
  211.    struct node *p;
  212. {
  213.  
  214.     if( p == NULL )
  215. /* we are at end of tree, need to add a node for this word */
  216.     {
  217.         if((p = alloc( sizeof( struct node ) )) == NULL)
  218.         {
  219.             printf("\nERROR - Out of memory");
  220.             exit( 1 );
  221.         }
  222.         if((p->word = alloc( strlen( w ) + 1 )) == NULL)
  223.         {
  224.             printf("\nERROR - Out of memory");
  225.             exit( 1 );
  226.         }
  227.         p->lower = NULL;
  228.         p->higher = NULL;
  229.         strcpy( p->word, w );
  230.         return( p );
  231.     }
  232.     else if( strcmp( p->word, w ) > 0 )
  233. /*  word 'w' is lower than the word at this node, look lower */
  234.         p->lower = treeload( w, p->lower );
  235.     else if( strcmp( p->word, w ) < 0 )
  236. /* word 'w' is higher than the word at this node, look higher */
  237.         p->higher = treeload( w, p->higher );
  238. /* if word 'w' is identical to the word at this node, we do */
  239. /* nothing. In all cases, we return the current node pointer. */
  240.     return( p );
  241. }
  242.  
  243.  
  244. /*--------------------------------------------------------------
  245. print() writes the words in alpha order back into the .DIC
  246. file.
  247. --------------------------------------------------------------*/
  248.  
  249. print( p )
  250.    struct node *p;
  251. {
  252. /* print all words lower than current node first */
  253.     if( p->lower != NULL )
  254.         print( p->lower );
  255.  
  256. /* print the word at the current node */
  257.     fprintf( inptr, "\s\n", p->word );
  258.  
  259. /* print all words higher than the current node */
  260.     if( p->higher != NULL )
  261.         print( p->higher );
  262. }
  263.  
  264.  
  265. /*-------------------------------------------------------------
  266. getcc() gets chars from the infile, and writes all but tildes
  267. to the outfile. This function does not disturb any high bits
  268. set for parity or formatting.
  269. -------------------------------------------------------------*/
  270.  
  271. getcc( fd )
  272.    int *fd;
  273. {
  274.    int c;
  275.     c = getc( fd );
  276.     if(((c & 0x7F) != '~') && (c != ERROR))
  277.         if( fputc( c, outptr ) == ERROR)
  278.         {
  279.             printf("\nERROR - Disk full - Pass 1");
  280.             exit( 1 );
  281.         }
  282.     return( c );
  283. }
  284.